<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\UserTask;
use App\Models\UserAmountLog;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class UserTaskController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new UserTask(), function (Grid $grid) {
            $grid->model()->where('content', '!=', '')->orderBy('id', 'desc');
            $grid->column('id')->sortable();
            $grid->column('user_id', '用户')->display(function ($user_id) {
                return \App\Models\User::find($user_id)->name ?? '';
            });
            $grid->column('task_id', '任务')->display(function ($task_id) {
                return \App\Models\Task::find($task_id)->name ?? '';
            });
            $grid->column('content', '图片')->image(50, 50);
            $grid->column('status', '审核状态')->select(\App\Models\UserTask::STATUS, true);
            $grid->column('created_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('status', '状态')->select(\App\Models\UserTask::STATUS);
            });

            $grid->disableCreateButton();
            $grid->disableDeleteButton();
            $grid->disableBatchActions();
            $grid->disableRowSelector();
            $grid->disableActions();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new UserTask(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('task_id');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new UserTask(), function (Form $form) {
            $form->text('status');

            $form->saving(function (Form $form) {
                $userTask = \App\Models\UserTask::query()->find($form->model()->id);
                $task = \App\Models\Task::query()->find($userTask->task_id);
                $user = \App\Models\User::query()->find($userTask->user_id);
                if ($form->status == \App\Models\UserTask::STATUS_Y) {
                    $user->amount += $task->award_number;
                    $user->save();
                    UserAmountLog::query()->create(
                        [
                            'user_id' => $userTask->user_id,
                            'amount' => $task->award_number,
                            'balance' => $user->amount,
                            'type' => 0,
                            'map_id' => $userTask->id,
                            'remark' => '悬赏任务',
                        ]
                    );
                }
            });
        });
    }
}
